package com.sghd.candy.module.element.service;

import com.sghd.candy.module.element.manager.ElementConfig;
import com.sghd.candy.module.element.manager.ElementConfigManager;
import com.sghd.pojo.StaticConfig;
import com.sghd.pojo.candy.fight.enums.BattleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Allen
 * @date 2018/12/03
 */
@Service
public class ElementConfigService {
    @Autowired
    ElementConfigManager elementConfigManager;

    public ElementConfig getDropElement(BattleResult battleResult) {
        List<ElementConfig> list = elementConfigManager.findAll();
        double random = Math.random();
        float currentFloat = 0F;
        float off = 1.0F;

        if (battleResult == BattleResult.LOSE) {
            off = off * StaticConfig.LOSE_OFF;
        }

        for (ElementConfig elementConfig : list) {
            if (random > currentFloat && random <= currentFloat + elementConfig.getDropRate() * off) {
                return elementConfig;
            } else {
                currentFloat = currentFloat + elementConfig.getDropRate() * off;
            }
        }
        return null;
    }
}
